/**
 * @author		FengChaoHua
 * @create		2018年6月8日 上午10:52:53
 * @project	huilun_wx
 * @desc		
 */
package top.wardyan.common.util;

import java.security.MessageDigest;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * @author FengChaoHua
 *
 */
public class MD5Util {
	
	
	//private static final String KEY = "myMw6qPt&3AD";
    
	
	/**
	 * md5加密
	 * @param plainText 如果=null,则视同=""
	 * @return
	 */
	public static String MD5(String plainText) {  
		if(plainText == null) {
			plainText = "";
		}
		// MD5一定支持的
        try {
           MessageDigest md = MessageDigest.getInstance("MD5");       
           md.update(plainText.getBytes());         
           byte b[] = md.digest();  
           
           int i;  
           StringBuffer buf = new StringBuffer("");  
           for (int offset = 0; offset < b.length; offset++) {  
        	   i = b[offset];  
        	   if (i < 0)  {
	               i += 256;  
	           }
        	   if (i < 16) {  
        		   buf.append("0");  
        	   }
        	   buf.append(Integer.toHexString(i));  
           }  
           return buf.toString();  
          //  System.out.println("32λ: " + buf.toString());// 32位  
          // System.out.println("16λ: " + buf.toString().substring(8, 24));// 16位  
        }   
        catch (Exception e) {  
          e.printStackTrace();  
        }  
        return "";
      }  
	
	
	
	// 加密
    public static String Encrypt(String sSrc, String sKey) throws Exception {
        if (sKey == null) {
            System.out.print("Key为空null");
            return null;
        }
        // 判断Key是否为16位
        if (sKey.length() != 16) {
            System.out.print("Key长度不是16位");
            return null;
        }
        byte[] raw = sKey.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));

        return Base64.getEncoder().encodeToString(encrypted);//此处使用BASE64做转码功能，同时能起到2次加密的作用。
    }

    // 解密
    public static String Decrypt(String sSrc, String sKey) throws Exception {
        try {
            // 判断Key是否正确
            if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = Base64.getDecoder().decode(sSrc);//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }
}
